package com.li.o2oSystem.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.li.o2oSystem.dao.LocalAuthDao;
import com.li.o2oSystem.dto.LocalAuthExecution;
import com.li.o2oSystem.entity.LocalAuth;
import com.li.o2oSystem.enums.LocalAuthStateEnum;
import com.li.o2oSystem.exceptions.LocalAuthOperationException;
import com.li.o2oSystem.service.LocalAuthService;
import com.li.o2oSystem.util.MD5;
@Service
public class LocalAuthServiceImpl implements LocalAuthService {
	@Autowired
	private LocalAuthDao localAuthDao;
	//根据用户名和密码查询用户信息
	@Override
	public LocalAuth getLocalAuthByUsernameAndPwd(String username, String password) {
		return localAuthDao.queryLocalByUsernameAndPwd(username, MD5.getMD5(password));
	}

	@Override
	public LocalAuth getLocalAuthByUserId(Long userId) {
		return localAuthDao.queryLocalByUserId(userId);
	}
//绑定微信账号
	@Override
	@Transactional
	public LocalAuthExecution bindLocalAuth(LocalAuth localAuth) throws LocalAuthOperationException {
		System.out.println(11);
		//空值判断
		if(localAuth==null||localAuth.getUsername()==null||localAuth.getPassword()==null||localAuth.getPersonInfo()==null||localAuth.getPersonInfo().getUserId()==null){
			return new LocalAuthExecution(LocalAuthStateEnum.NULL_AUTH_INFO);
		}
		//查询是否已经绑定微信账号
		LocalAuth tempAuth = localAuthDao.queryLocalByUserId(localAuth.getPersonInfo().getUserId());
		//判断是否为空
		if(tempAuth != null){
			//如果绑定则，直接退出
			return new LocalAuthExecution(LocalAuthStateEnum.ONLY_ONE_ACCOUNT);
		}
		try {
			//如果没有绑定，则创建绑定
			localAuth.setCreateTime(new Date());
			localAuth.setLastEditTime(new Date());
			//对密码进行MD5加密
			localAuth.setPassword(MD5.getMD5(localAuth.getPassword()));
			//执行插入本地账号信息
			int effectNum = localAuthDao.insertLocalAuth(localAuth);
			//判断是否插入成功
			if(effectNum <= 0){
				throw new LocalAuthOperationException("账号绑定失败");
			}else{
				return new LocalAuthExecution(LocalAuthStateEnum.SUCCESS,localAuth);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new LocalAuthOperationException("绑定微信账号异常："+e.getMessage());
		}
	}
//修改密码
	@Override
	@Transactional
	public LocalAuthExecution modifyLocalAuth(Long userId, String username, String password, String newPassword)
			throws LocalAuthOperationException {
		//非空判断
		if(userId != null && username != null && password != null && newPassword != null && !password.equals(newPassword)){
			try {
				//更新密码，并对新密码加密
				int effectNum = localAuthDao.updateLocalAuth(userId, username, MD5.getMD5(password), MD5.getMD5(newPassword), new Date());
				//判断是否更新成功
				if(effectNum <= 0){
					throw new LocalAuthOperationException("修改密码失败");
				}
				return new LocalAuthExecution(LocalAuthStateEnum.SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				throw new LocalAuthOperationException("修改密码异常："+e.getMessage());
			}
		}
		else{
			return new LocalAuthExecution(LocalAuthStateEnum.NULL_AUTH_INFO);
		}
	}

}
